package game.services.chat.handler;

import com.google.protobuf.GeneratedMessageV3;
import dou.net.socket.IClientSocket;
import game.services.MsgID;
import game.socket.AbstractMessageHandler;
import game.socket.NotifyUtil;
import game.socket.RemoteClientSocket;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import protocol.Chat;

import java.nio.ByteBuffer;

public class ChatSendMsgHandler extends AbstractMessageHandler
{
    private static Logger logger = LogManager.getLogger("GameServer");

    @Override
    public void messageHandler(int msgID, int msgNum, GeneratedMessageV3 message, IClientSocket socket) throws Exception
    {
        Chat.Req_ChatMsg msgInfo = (Chat.Req_ChatMsg)message;

        // TODO : 这里可以对聊天内容进行保存

        // 请求返回
        Chat.Revc_ChatMsg.Builder recvBuilder = Chat.Revc_ChatMsg.newBuilder();
        Chat.Revc_ChatMsg recvInfo = recvBuilder.build();
        ByteBuffer recvBytes = encodeMessage(msgID, msgNum, recvInfo, false);
        socket.send(recvBytes);

        // 广播给其它客户端
        Chat.Push_ChatMsg.Builder builder = Chat.Push_ChatMsg.newBuilder();
        builder.setName(msgInfo.getName());
        builder.setContent(msgInfo.getContent());
        Chat.Push_ChatMsg sendInfo = builder.build();
        ByteBuffer sendBytes = encodeMessage(MsgID.Push_ChatMsg.value(), 0, sendInfo, false);
        NotifyUtil.notifyALLWithoutSelf((RemoteClientSocket) socket, sendBytes);
    }
}
